package com.ytzl.itrip.utils.common;

import com.cloopen.rest.sdk.CCPRestSmsSDK;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Random;

/**
 * 向注册手机号发送手机验证码的工具类
 */
@Component
public class SendTemSMS {

    @Resource
    private  RedisAPI redisAPI;

    /**
     *
     * @return  4位随机验证码
     */
    public String code(){
        String str = "1234567890";
        StringBuffer sb = new StringBuffer(4);
        for (int i=0;i<4;i++){
            char c = str.charAt(new Random().nextInt(str.length()));
            sb.append(c);
        }
        String codeId = sb.toString();
        return codeId;
    }

    /**
     * 返回向手机发送的验证码
     * @param phone  要发送验证码的手机号
     * @param model   要使用的模板
     * @param timeout  验证码过期时间
     * @return  codeId  四位随机数字验证码
     */
    public void sendSMS(String phone,String model,String timeout){
        String codeId = code();
        HashMap<String, Object> result = null;
        CCPRestSmsSDK restAPI = new CCPRestSmsSDK();
        restAPI.init("app.cloopen.com", "8883");
        restAPI.setAccount("8aaf0708646d63ec0164875798f811bb",
                "c26b6c30c5c14f509c517f40eaf6cbeb");
        restAPI.setAppId("8aaf0708646d63ec01648757994a11c1");
        restAPI.sendTemplateSMS(phone,model,new String[]{codeId,timeout});
        redisAPI.set("activation:["+phone+"]",codeId,60*5);
    }
}
